package net.vinote.smart.platform.tool.image;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 实现截屏功能
 * 
 * @author Seer
 *
 */
public class ScreenCapture {

	public static byte[] captureBytes() {
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(capture(), "png", out);
			return out.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static BufferedImage capture(int x, int y, int width, int height)
			throws AWTException {
		Rectangle rectangle = new Rectangle(x, y, width, height);
		Robot robot = new Robot();
		return robot.createScreenCapture(rectangle);
	}

	public static BufferedImage capture() throws AWTException {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		return capture(0, 0, screenSize.width, screenSize.height);
	}

	public static void main(String[] args) throws FileNotFoundException,
			IOException, AWTException {
		ImageIO.write(capture(), "png", new FileOutputStream("D:\\temp.png"));
	}
}
